﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using System.Data.Linq;
using System.Linq;

namespace BISPOS
{
    public partial class DailySlipReportUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        InvoiceRepository invoiceRepository = new InvoiceRepository();
        PaymentRepository paymentRepository = new PaymentRepository();
        public DailySlipReportUserControl()
        {
            InitializeComponent();
            dateEdit.DateTime = DateTime.Now;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            cashListBoxControl.Items.Clear();
            creditListBoxControl.Items.Clear();
            DateTime date = dateEdit.DateTime;
            IEnumerable<Invoice> invoices = invoiceRepository.GetActiveInvoices(date.Date,date.Date).OrderBy(x=>x.Id);
            List<InvoiceItem> excludeTaxItems = new List<InvoiceItem>();
            List<InvoiceItem> includeTaxItems = new List<InvoiceItem>();
            foreach(var invoice in invoices)
            {
                foreach(var item in invoice.InvoiceItems)
                {
                    if(item.Stock.Product.IsTaxed)
                    {
                        includeTaxItems.Add(item);
                    }
                    else
                    {
                        excludeTaxItems.Add(item);
                    }
                }
            }
            //Cash
            cashSlipLabelControl.Text = "เลขที่เอกสาร " + invoices.FirstOrDefault().IdText + " - " + invoices.LastOrDefault().IdText;
            cashListBoxControl.Items.Add("สรุปขายสินค้าเงินสด");
            decimal totalExcludeTax = 0;
            decimal totalIncludeTax = 0;
            decimal totalVat = 0;
            foreach(var item in paymentRepository.CashPaymentTypes())
            {
                decimal excludeTaxAmount = excludeTaxItems.Where(x=>x.Invoice.PaymentTypeId == item.Id).Sum(x=>x.Price.Amount*(decimal)x.Quantity);
                totalExcludeTax += excludeTaxAmount;
                decimal includeTaxAmount = includeTaxItems.Where(x=>x.Invoice.PaymentTypeId == item.Id).Sum(x=>x.Price.Amount*(decimal)x.Quantity);
                totalIncludeTax += includeTaxAmount;
                decimal vat = includeTaxAmount *(decimal)(7.0000/107.0000);
                totalVat += vat;
                
                cashListBoxControl.Items.Add(" -" + item.Name);
                cashListBoxControl.Items.Add(
                      "    สินค้ายกเว้นภาษี: " + excludeTaxAmount.ToString("n2").PadRight(10) + " บาท"
                    + "    มูลค่าสินค้า: " + (includeTaxAmount - vat).ToString("n2").PadRight(10) + " บาท"
                    + "    ภาษีมูลค่าเพิ่ม 7%: " + vat.ToString("n2").PadRight(7) + " บาท");
            }
            cashListBoxControl.Items.Add("รวมทั้งหมด");
            cashListBoxControl.Items.Add(
                        "    สินค้ายกเว้นภาษี: " + totalExcludeTax.ToString("n2").PadRight(10) + " บาท"
                    + "    มูลค่าสินค้า: " + (totalIncludeTax - totalVat).ToString("n2").PadRight(10) + " บาท"
                    + "    ภาษีมูลค่าเพิ่ม 7%: " + totalVat.ToString("n2").PadRight(10) + " บาท");
            //End Cash

            //Credit
            CreditSlipLabelControl.Text = "เลขที่เอกสาร " + invoices.FirstOrDefault().IdText + " - " + invoices.LastOrDefault().IdText;
            creditListBoxControl.Items.Add("สรุปขายสินค้าเงินเชื่อ");
            totalExcludeTax = 0;
            totalIncludeTax = 0;
            totalVat = 0;
            foreach (var item in paymentRepository.CreditPaymentTypes())
            {
                decimal excludeTaxAmount = excludeTaxItems.Where(x => x.Invoice.PaymentTypeId == item.Id).Sum(x => x.Price.Amount*(decimal)x.Quantity);
                totalExcludeTax += excludeTaxAmount;
                decimal includeTaxAmount = includeTaxItems.Where(x => x.Invoice.PaymentTypeId == item.Id).Sum(x => x.Price.Amount*(decimal)x.Quantity);
                totalIncludeTax += includeTaxAmount;
                decimal vat = includeTaxAmount * (decimal)(7.0000 / 107.0000);
                totalVat += vat;

                creditListBoxControl.Items.Add(" -" + item.Name);
                creditListBoxControl.Items.Add(
                      "    สินค้ายกเว้นภาษี: " + excludeTaxAmount.ToString("n2").PadRight(10) + " บาท"
                    + "    มูลค่าสินค้า: " + (includeTaxAmount - vat).ToString("n2").PadRight(10) + " บาท"
                    + "    ภาษีมูลค่าเพิ่ม 7%: " + vat.ToString("n2").PadRight(10) + " บาท");
            }
            creditListBoxControl.Items.Add("รวมทั้งหมด");
            creditListBoxControl.Items.Add(
                        "    สินค้ายกเว้นภาษี: " + totalExcludeTax.ToString("n2").PadRight(10) + " บาท"
                    + "    มูลค่าสินค้า: " + (totalIncludeTax - totalVat).ToString("n2").PadRight(10) + " บาท"
                    + "    ภาษีมูลค่าเพิ่ม 7%: " + totalVat.ToString("n2").PadRight(10) + " บาท");
            //End Credit

        }

        private void printCashButton_Click(object sender, EventArgs e)
        {
            DateTime date = dateEdit.DateTime;
            IEnumerable<Invoice> invoices = invoiceRepository.GetActiveInvoices(date.Date, date.Date).OrderBy(x => x.Id);
            
            List<PaymentType> paymentTypes = new List<PaymentType>();
           
            foreach (var paymentType in paymentRepository.CashPaymentTypes())
            {
                PaymentType temp = new PaymentType
                {
                    IsCaptitalPrice = paymentType.IsCaptitalPrice,
                    Name = paymentType.Name,
                    IsCredit = paymentType.IsCredit
                };
                foreach (var invoice in invoices.Where(x => x.PaymentType.Id == paymentType.Id))
                {
                    temp.Invoices.Add(invoice);
                }
                paymentTypes.Add(temp);
            }
            Report.DailySlipReport report = new Report.DailySlipReport();
            report.DataSource = paymentTypes.AsEnumerable();
            report.numberText.Value = "เลขที่เอกสาร " + invoices.FirstOrDefault().IdText + " - " + invoices.LastOrDefault().IdText;
            report.paymentTypeText.Value = "สรุปขายสินค้าเงินสด";
            report.RequestParameters = false;
            report.ShowPreview();
           

        }

        private void printCreditButton_Click(object sender, EventArgs e)
        {
            DateTime date = dateEdit.DateTime;
            IEnumerable<Invoice> invoices = invoiceRepository.GetActiveInvoices(date.Date, date.Date).OrderBy(x => x.Id);

            List<PaymentType> paymentTypes = new List<PaymentType>();

            foreach (var paymentType in paymentRepository.CreditPaymentTypes())
            {
                PaymentType temp = new PaymentType
                {
                    IsCaptitalPrice = paymentType.IsCaptitalPrice,
                    Name = paymentType.Name,
                    IsCredit = paymentType.IsCredit
                };
                foreach (var invoice in invoices.Where(x => x.PaymentType.Id == paymentType.Id))
                {
                    temp.Invoices.Add(invoice);
                }
                paymentTypes.Add(temp);
            }
            Report.DailySlipReport report = new Report.DailySlipReport();
            report.DataSource = paymentTypes.AsEnumerable();
            report.numberText.Value = "เลขที่เอกสาร " + invoices.FirstOrDefault().IdText + " - " + invoices.LastOrDefault().IdText;
            report.paymentTypeText.Value = "สรุปขายสินค้าเงินเชื่อ";
            report.RequestParameters = false;
            report.ShowPreview();
        }
    }
}
